package main

import (
	"flag"
	"fmt"
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/logs"
	"github.com/astaxie/beego/orm"
	"github.com/cnlh/httpMonitor/lib"
	_ "github.com/cnlh/httpMonitor/models"
	_ "github.com/cnlh/httpMonitor/routers"
	_ "github.com/go-sql-driver/mysql"
	_ "github.com/mattn/go-sqlite3"
	"log"
	"os"
	"time"
)

func unzip() {
	dirs := []string{"static", "conf", "views"} // 设置需要释放的目录
	Exists := func(path string) bool {
		_, err := os.Stat(path) //os.Stat获取文件信息
		if err != nil {
			if os.IsExist(err) {
				return true
			}
			return false
		}
		return true
	}
	for _, dir := range dirs {
		if Exists("./" + dir) {
			continue
		}
		if err := RestoreAssets("./", dir); err != nil {
			continue
		}
	}
}

func init() {
	unzip()
	lib.ReadConf()
	dbType := beego.AppConfig.String("dbType")
	if dbType=="mysql" {
		dbHost := beego.AppConfig.String("dbHost")
		dbPort := beego.AppConfig.String("dbPort")
		dbUser := beego.AppConfig.String("dbUser")
		dbPassword := beego.AppConfig.String("dbPassword")
		dbName := beego.AppConfig.String("dbName")
		dsn := dbUser + ":" + dbPassword + "@tcp(" + dbHost + ":" + dbPort + ")/" + dbName + "?charset=utf8&loc=Asia%2FShanghai"
		orm.RegisterDriver("mysql", orm.DRMySQL)
		orm.RegisterDataBase("default", "mysql", dsn)
		orm.RunSyncdb("default", false, true)
	} else {
		orm.RegisterDriver("sqlite", orm.DRSqlite)
		orm.RegisterDataBase("default", "sqlite3", "./conf/monitor.db")
		orm.RunSyncdb("default", false, true)
	}
}
func main() {
	password := flag.String("psd", "", "password")
	flag.Parse()
	if *password != "" {
		json := make(map[string]string)
		json["sys::password"] = lib.Str2md5(*password)
		lib.SetConf(json)
		fmt.Println("密码设置成功")
		return
	}
	orm.DefaultTimeLoc=time.Local
	orm.RunCommand()
	lib.InitJob()
	beego.AddFuncMap("isColor", lib.IsColor)
	if err := lib.ReadConf(); err != nil {
		log.Fatalf("配置文件读取错误")
	}
	logs.SetLogger(logs.AdapterFile, `{"filename":"project.log","level":5,"maxlines":0,"maxsize":0,"daily":false,"maxdays":10,"color":true}`)
	beego.BConfig.WebConfig.Session.SessionOn = true
	beego.BConfig.RecoverPanic=true
	beego.Run()
}
